grid
Every other container on this manual is either a flat run of slots (array<T>/list<T>) or reached through its own named functions with no indexing at all (stack<T>/queue<T>/ map<K,V>/set<T>). grid<T> is its own third shape: a fixed block of cells arranged in rows and columns, indexed with two numbers instead of one. Structurally it behaves like array<T>, not list<T> - every cell exists from the moment you declare it, and there is no grid_add/grid_remove to change how many there are, the same way a tic-tac-toe board or a tilemap is never "grown" one square at a time.
grid<T> has sixteen functions of its own on this manual, besides reading and writing a cell:
| Function | What it does | It gives back |
|---|---|---|
grid_create(width, height, fill) | makes a grid with every cell already filled | the new grid |
grid_width(grid) | counts the columns | the count |
grid_height(grid) | counts the rows | the count |
grid_fill(grid, value) | sets every cell to value | nothing |
grid_fill_region(grid, x1, y1, x2, y2, value) | sets a block of cells to value, both corners included | nothing |
grid_get_region_sum(grid, x1, y1, x2, y2) | adds up the cells in a block | the total |
grid_get_region_max(grid, x1, y1, x2, y2) | finds the biggest cell in a block | the biggest value |
grid_get_region_mean(grid, x1, y1, x2, y2) | averages the cells in a block (a grid of whole numbers cuts the answer off) | the average |
grid_copy(grid) | makes a second grid just like it | the new grid |
grid_sort(grid, column, ascending) | puts the numbers or the text of each column (or each row) in order, each one on its own | nothing |
grid_sort_rows(grid, by_column, ascending) | puts the rows in order by the value in one column, each row staying whole | nothing |
grid_shuffle(grid) | mixes up the cells, each one on its own | nothing |
grid_shuffle_rows(grid) | mixes up the order of the rows, each row staying as it is | nothing |
grid_shuffle_columns(grid) | mixes up the order of the columns, each column staying as it is | nothing |
grid_foreach(grid, script) | runs one of your scripts once for each cell, row by row | nothing |
grid_filter(grid, script) | makes a list of just the cell values a script says yes to | the new list |
A grid has two sizes, not one, so it has two functions for them, named for the two numbers grid_create takes. Nothing can add or take away a cell, so there is no function to do that.
grid_sort sorts every column (or row) on its own, so the cells of a row do not stay together afterwards. For a table where each row means something as a whole, a high-score table say, use grid_sort_rows: it puts the rows in order by one column and keeps every row together. Both work on a grid of numbers or of text. grid_filter gives back a list, not a grid: the cells that pass no longer have rows and columns to sit in.
The three grid_get_region_ functions only work on a grid of numbers: whole numbers or decimals. A grid of text, yes-or-no values or structs is refused when the project is checked. The functions with region in the name take a block written as two corners, (x1, y1) at the top-left and (x2, y2) at the bottom-right, and both corners are included. grid_fill_region has the full rules for a block: corners the wrong way round, a block that runs off the grid, and how a mistake typed in the code is caught.
A grid needs its own width and height, not just a total cell count, because indexing it (below) needs to know how many columns each row has. Write the capacity as WxH:
/// @type {grid<int>}
/// @capacity 3x3
var board = [];declares a 3-wide, 3-tall grid - 9 cells, all starting at their type's own default (0 for int). A bare /// @capacity 9 still declares a real 9-cell grid, exactly like it would for any other container - it simply leaves the grid un-indexable, since nothing said how those 9 cells are arranged into rows. The compiler catches this the moment [# x, y] is actually tried on a grid with no WxH shape, naming the fix - never a wrong cell. See Variables, types and scope for the full /// @capacity tag and how it applies to every container, not just this one.
You can also write the size without a comment. Put both numbers inside the type, after the name: var board: grid<int, 3, 3> = []; is the same 3-by-3 grid. The first number is the width and the second is the height, so a grid always gets both (grid<int, 3> is refused). And grid_create makes one whose cells start as something other than the default: var board = grid_create(3, 3, -1); is a 3-by-3 grid full of -1. Both ways are fully supported, and the tags are not going anywhere - Writing a type out has the whole table.
grid[# x, y] reads or writes one cell - x is the column, y is the row, always in that order, and always with a space after the # ([# x, y], not [#x, y] - written tight against a digit or a letter a-f, #1/#a reads as the start of a #RRGGBB color literal instead). It works on either side of =, the same as array<T>/list<T>'s own [i]:
board[# 1, 1] = 5; // write the centre cell of a 3x3 grid
var centre = board[# 1, 1]; // read it back - 5Writing past a grid's own width or height is caught the same way every other bounded container on this manual is - it does not corrupt a neighboring cell or crash, and a development build reports it.
/// @type {grid<int>}
/// @capacity 3x3
var board = [];
board[# 1, 1] = 5;
var center = board[# 1, 1]; // 5The same grid without the tags:
var board = grid_create(3, 3, 0);
board[# 1, 1] = 5;
var center = board[# 1, 1]; // 5



This is this page's own board, shown as three rows of three cells, before board[# 1, 1] = 5 (every cell still 0) and after it (only the center cell changed). Showing all nine cells, not just the one that changed, is what proves the write reached exactly the cell asked for and nowhere else - the same guarantee array<T>/list<T>'s own single-number indexing gives, now working in two dimensions.
Data containers covers where grid<T> sits next to the other six - array<T>, list<T>, stack<T>, queue<T>, map<K,V> and set<T>, and the rules they all share.